/*
 * @Author: 0x9DEFA478
 * @Date: 2021-08-26 23:43:03
 * @LastEditTime: 2021-09-05 16:55:38
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "GUI_User.h"
#include "H_Font.h"
#include "H_StringItem.h"


//要渲染的位图
static H_Graphics_Bitmap* volatile DrawStringBitmap=NULL;

static void L1_DrawString(H_Graphics_Bitmap* bitmap,char* String,H_Font* Font,int X,int Y,int mX,int mY,int isReverse){
  H_Font* rFont;
  int x;
  int y;
  int rWidth;
  unsigned short unicode;
  void* fontBitmap;
  unsigned char c;
  unsigned char c1;
  unsigned char c2;
  H_Graphics_Area_Param Area_Param;
  H_Graphics_Bitmap Src;

  Area_Param.D=bitmap;
  Area_Param.S=&Src;
  x=X;
  y=Y;

  while(String[0]!='\0')
  {
    c=String[0];

    if((c&0x80U)!=0){
      if((c&0xC0U)==0xC0U){
        if((c&0x20U)==0x20U){
        
          if((c&0x10U)==0x10U){
            //不支持过长的UTF8(此时已经超过16Bit)
            String=&String[4];
            continue;
          }else{
            //1110 16Bit

            c1=String[1];
            if((c1&0xC0U)!=0x80U){
              //第二个字节错误
              String=&String[3];
              continue;
            }

            c2=String[2];
            if((c2&0xC0U)!=0x80U){
              //第三个字节错误
              String=&String[3];
              continue;
            }

            unicode=((unsigned short)c&0x0FU)<<12;
            unicode|=((unsigned short)c1&0x3FU)<<6;
            unicode|=c2&0x3FU;
            String=&String[3];
          }

        }else{
          //110 11Bit

          c1=String[1];

          if((c1&0xC0U)!=0x80U){
            //第二个字节错误
            String=&String[2];
            continue;
          }

          unicode=((unsigned short)c&0x1FU)<<6;
          unicode|=c1&0x3FU;

          String=&String[2];
        }
      }else{
        //无效unicode字符
        String=&String[1];
        continue;
      }
    }else{
      //0 7bit
      unicode=c;

      String=&String[1];
    }

    if((unicode=='\r')&&(String[0]=='\n')){
      String=&String[1];

      x=0;
      y+=Font->FontHeight;
      if(y>mY){
        return;
      }

      continue;
    }

    if((unicode=='\r')||(unicode=='\n')){

      x=0;
      y+=Font->FontHeight;
      if(y>mY){
        return;
      }

      continue;
    }

    rFont=NULL;
    //获取字符
    fontBitmap=Font->GetBitmap(Font,&rFont,unicode,&rWidth);
    if(fontBitmap!=NULL){
      if(rFont==NULL){
        while(1)
        {
          //Err
        }
      }

      if(rWidth==rFont->FontMaxWidth-1){
        rWidth=rFont->FontMaxWidth;
      }

      Src.Bitmap=fontBitmap;
      Src.Height=rFont->FontHeight;
      Src.Width=rFont->FontMaxWidth;
      Area_Param.sX=0;
      Area_Param.sY=0;

      Area_Param.dX=x;
      Area_Param.dY=y;

      if((x+rWidth-1)>mX){
        Area_Param.Width=mX+1-x;
      }else{
        Area_Param.Width=rWidth;
      }
      if(Area_Param.Width<0){
        return;
      }

      if((y+rFont->FontHeight-1)>mY)
      {
        Area_Param.Height=mY+1-y;
      }else{
        Area_Param.Height=rFont->FontHeight;
      }

      if(isReverse!=0){
        H_Graphics_L1Vertical_CopyTo_L1Vertical_Reverse(&Area_Param);
      }else{
        H_Graphics_L1Vertical_CopyTo_L1Vertical(&Area_Param);
      }

      x+=rWidth;
    }

  }

}

void GUI_User_SetDrawStringBitmap(H_Graphics_Bitmap* Bitmap){
  DrawStringBitmap=Bitmap;
}

H_Graphics_Bitmap* GUI_User_GetDrawStringBitmap(){
  return DrawStringBitmap;
}

void GUI_User_DrawString(char *String,void *Font,int Status,int X,int Y,int MaxX,int MaxY){
  H_Graphics_Bitmap* bitmap;

  bitmap=DrawStringBitmap;

  if(bitmap==NULL){
    return;
  }

  switch(Status){
    case vH_StringItem_Status_Default:
      L1_DrawString(bitmap,String,Font,X,Y,MaxX,MaxY,0);
      break;
    case vH_StringItem_Status_HighLight:
      L1_DrawString(bitmap,String,Font,X,Y,MaxX,MaxY,-1);
      break;
    case vH_StringItem_Status_Select:
      L1_DrawString(bitmap,String,Font,X,Y,MaxX,MaxY,-1);

      if((String[0]==' ')&&(String[1]==' ')){
        L1_DrawString(bitmap,"->",Font,X,Y,MaxX,MaxY,-1);
      }
      
      break;
    
    default:
      break;
  }
}

